package com.app.service.impl;

import com.app.mapper.jiuquan.PolicyMapper;
import com.app.mapper.jiuquan.TbcConfigMapper;
import com.app.model.PolicyModel;
import com.app.model.TbcConfigModel;
import com.app.service.PolicyService;
import com.app.util.StringTool;
import com.app.vo.PolicyVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//政策
@Service
public class PolicyServiceImpl extends ServiceImpl<PolicyMapper, PolicyModel> implements PolicyService {
    @Autowired
    private PolicyMapper policyMapper;

    @Autowired
    private TbcConfigMapper tbcConfigMapper;

    @Override
    public PolicyVo getListByModel(PolicyModel model) {

        QueryWrapper<PolicyModel> queryWrapper = new QueryWrapper<>();
        if (StringTool.isNotEmpty(model.getKeyWord())) {
            queryWrapper.eq("title", model.getKeyWord());
        }
        if (model.getType() != null && model.getType() > 0) {
            queryWrapper.eq("type", model.getType());
        }
        if (model.getUserType() != null && model.getUserType() > 0) {
            queryWrapper.eq("user_type", model.getUserType());
        }

        if (StringTool.isNotEmpty(model.getStartTime())) {
            queryWrapper.apply(" TO_DAYS(crt_time) >= TO_DAYS('" + model.getStartTime() + "')");
        }
        if (StringTool.isNotEmpty(model.getEndTime())) {
            queryWrapper.apply(" TO_DAYS(crt_time) <= TO_DAYS('" + model.getEndTime() + "')");
        }
        Page<PolicyModel> pages = new Page<>();
        pages.setCurrent(model.getPage());
        pages.setSize(model.getLimit());
        Page<PolicyModel> configModelPage = policyMapper.selectPage(pages, queryWrapper);
        PolicyVo vo = new PolicyVo();
        for (int i = 0; i < configModelPage.getRecords().size(); i++) {
            TbcConfigModel configModel = tbcConfigMapper.selectById(configModelPage.getRecords().get(i).getType());
            if (configModel != null) {
                configModelPage.getRecords().get(i).setTypeText(configModel.getName());
            }
            Integer userType = configModelPage.getRecords().get(i).getUserType();
            if(userType!=null)
            {
                if (userType == 1) {
                    configModelPage.getRecords().get(i).setUserTypeText("个人");
                }
                if (userType == 2) {
                    configModelPage.getRecords().get(i).setUserTypeText("企业");
                }
            }

        }
        vo.setList(configModelPage.getRecords());
        vo.setCount((int) configModelPage.getTotal());
        return vo;
    }
}
